<?php
/**
 * Created by PhpStorm.
 * User: 67061
 * Date: 2019/10/17
 * Time: 14:15
 */

namespace app\admin\controller;


use app\admin\model\CepingCategoryModel;
use app\admin\model\CepingModel;
use cmf\controller\AdminBaseController;
use think\Db;

class CepingController extends AdminBaseController
{
    public function index()
    {
        $content = hook_one('admin_link_index_view');
        if (!empty($content)) {
            return $content;
        }

        /**搜索条件**/
        $category_id = $this->request->param('category_id');

        $cepings = Db::name('ceping')
            ->where(function ($query) use ($category_id) {
                if ($category_id) {
                    $query->where('category_id', $category_id);
                }
            })
            ->order("id DESC")
            ->paginate(10);
        $cepings->appends(['category_id' => $category_id]);
        // 获取分页显示
        $page = $cepings->render();

        $Category    = new CepingCategoryModel();
        $categories = $Category->select();
        $list = array();
        foreach ($cepings as $v){
            foreach ($categories as $val){
                if ($v['category_id'] == $val['id']){
                    $v['category'] = $val['name'];
                }
            }
            $list[] = $v;
        }

        $this->assign("page", $page);
        $this->assign('categories', $categories);
        $this->assign('category_id', $category_id);
        $this->assign('cepings', $list);
        return $this->fetch();
    }

    public function add()
    {
        $Category    = new CepingCategoryModel();
        $categories = $Category->select();
        $this->assign('categories', $categories);
        return $this->fetch();
    }


    public function addPost()
    {
        $data = $this->request->param();
        $CepingModel = new CepingModel();
//        $result = $this->validate($data, 'Ceping');
//        if ($result !== true) {
//            $this->error($result);
//        }
        $data['create_time'] = time();
        $data['update_time'] = time();
        $CepingModel->allowField(true)->save($data);

        $this->success("添加成功！", url("Ceping/index"));
    }

    public function edit()
    {
        $Category    = new CepingCategoryModel();
        $categories = $Category->select();
        $this->assign('categories', $categories);
        $id = $this->request->param('id', 0, 'intval');
        $CepingModel = new CepingModel();
        $ceping = $CepingModel->get($id);
        $this->assign('ceping', $ceping);
        return $this->fetch();
    }

    public function editPost()
    {
        $data = $this->request->param();
        $CepingModel = new CepingModel();
//        $result = $this->validate($data, 'Link');
//        if ($result !== true) {
//            $this->error($result);
//        }
        $data['update_time'] = time();
        $CepingModel->allowField(true)->isUpdate(true)->save($data);

        $this->success("保存成功！", url("Ceping/index"));
    }

    /**
     * 删除友情链接
     * @adminMenu(
     *     'name'   => '删除友情链接',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除友情链接',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        CepingModel::destroy($id);
        $this->success("删除成功！", url("ceping/index"));
    }

    public function category_add()
    {
        return $this->fetch();
    }

    public function category_add_post()
    {

    }
}
